<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">

    <title>选择企业</title>
    <base href="${rc.contextPath}/">
    <#include "/commons/stylesheets.ftl">
    <!-- Data Tables -->
    <link href="bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <style type="text/css">
        #toolbar button {
            margin-right: 10px;
        }
        span.field {
            font-weight: bold;
            padding-right: 10px;
        }
    </style>
    <script type="text/javascript">
        var CompanyType = {"1":"银行","2":"4S店","3":"保险公司","4":"修理厂","5":"汽配厂商","6":"其他"};
        function search(){
            $("#table").bootstrapTable("refresh",{url:"${rc.contextPath}/company/search?"+$("#form").serialize()});
            initCheck();
        }



        var Organizations={};
        function initOrganization(){
            $.getJSON("company/queryOrganization", function (list) {
                var parent;
                for (var i = 0; i < list.length; i++) {
                    row = list[i];
                    Organizations[row.organizationId] = row.name;
                }
            });
        }
        function pageInit(){
            initOrganization();
        }


        function formatCompanyType(value,row,index){
            return CompanyType[value]
        }

        function formatOrganization(value,row,index){
            return Organizations[value]
        }

        function refreshTable(){
            $("#table").bootstrapTable("refresh");
        }

        function queryParams(params){
            return params;
        }

        function cancelBind(){
            var  rows = $("#table").bootstrapTable("getSelections");
            if(rows.length == 0){
                layer.msg("请选择一条记录");
                return false;
            }
            var companyIds = $.map(rows,function(row){ return row.id});

            window.parent.delCompany(companyIds);
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }

        //绑定企业
        function addCompany(){
            var  rows = $("#table").bootstrapTable("getSelections");
            if(rows.length == 0){
                layer.msg("请选择一条记录");
                return false;
            }
            var ids = $.map(rows,function(row){ return row.id});
            //从选择的企业中过滤已经绑定的企业
            var selectCompanyIds = $("#selectCompanyIds").val();
            var curCompanyArr= new Array();
            curCompanyArr = selectCompanyIds.split(',');

            var newArr= new Array();
            newArr = ids;
            for(i in curCompanyArr) {
                var cId = Number(curCompanyArr[i]);
                var index = $.inArray(cId,newArr);
                if(index>=0){
                    newArr.splice(index,1);
                }
            }
            window.parent.refreshCompany(newArr);

            var activityId = $("#activityId").val();

            //编辑活动时，直接绑定企业
            if(activityId) {
                var handleResponse = function(resp){
                    if (resp.resultCode == "success") {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        layer.msg('添加失败');
                    }
                };
                $.ajax({
                   url:"activity/bindCompanyActivity",traditional:true,data:{activityId:activityId,companyIds:newArr},type:'post',success:handleResponse
               });
           } else {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
           }

        }
    </script>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form id="form" class="form-inline">
                        <input type="hidden" id="isShowButton" name="isShowButton" value="${isShowButton!''}"/>
                        <input type="hidden" id="activityId" name="activityId" value="${activityId!''}"/>
                        <input type="hidden" id="selectCompanyIds" name="selectCompanyIds" value="${selectCompanyIds!''}"/>

                            <div class="form-group">
                                <label class="control-label">名称：</label>
                                <input type="text" placeholder="" name="companyName" class="form-control">
                            </div>
                            <div class="form-group">
                                <label class="control-label">联系人：</label>
                                <input type="text" placeholder="" name="contactName" class="form-control">
                            </div>
                            <div class="form-group">
                                <label class="control-label">类型${total!''}：</label>
                                <select name="companyType" class="form-control" >
                                    <option value="">所有</option>
                                    <option value="1">银行</option>
                                    <option value="2">4S店</option>
                                    <option value="3">保险公司</option>
                                    <option value="4">修理厂</option>
                                    <option value="5">汽配厂商</option>
                                    <option value="6">其他</option>
                                </select>
                            </div>

                        </form>

                        <div id="toolbar">
                            <button class="btn btn-primary" type="button" style="margin-left: 20px;" onclick="search()">搜索</button>
                            <button class="btn btn-primary" type="button" style="margin-left: 20px;" onclick="cancelBind()">解绑</button>

                        </div>
                        <table id="table"
                               data-detail-view="false"
                               data-query-params="queryParams"
                               data-striped="true"
                               data-icons-prefix="fa"
                               data-toolbar="#toolbar"
                               data-data-field="list"
                               data-search="false"
                               data-show-refresh="false"
                               data-show-columns="false"
                               data-show-export="false"
                               data-side-pagination="server"
                               data-pagination="true"
                               data-page-size="15"
                               data-page-list="[10,15,20,30]"
                               data-url="${rc.contextPath}/company/searchSelectCompany?selectCompanyIds=${selectCompanyIds!''}"
                               >
                            <thead>
                                <tr>
                                    <th data-checkbox="true" >id</th>
                                    <th data-field="companyName">企业名称</th>
                                    <th data-field="companyType" data-formatter="formatCompanyType">类型</th>
                                    <th data-field="organizationId" data-formatter="formatOrganization">区域</th>
                                    <th data-field="contactPerson1Name">联系人</th>
                                    <th data-field="contactPerson1Position">职位</th>
                                    <th data-field="contactPerson1Phone">电话</th>
                                    <th data-field="address">地址</th>
                                </tr>
                            </thead>
                            <tbody>

                            </tbody>

                        </table>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <#include "/commons/scripts.ftl">
    <script src="layer/layer.js"></script>
    <script src="js/app.js"></script>
    <script src="js/template-native.js"></script>

    <script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
    <script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>

    <!-- Page-Level Scripts -->
    <script>

    //从选择的企业中过滤已经绑定的企业
    function initCheck(){
        var selectCompanyIds = $("#selectCompanyIds").val();
        var curCompanyArr= new Array();
        curCompanyArr = selectCompanyIds.split(',');
        var newArr= new Array();
        for(i in curCompanyArr) {
            var cId = curCompanyArr[i] + "";
            newArr.push(Number(cId));
        }
        console.log(newArr);
        //setTimeout(checkBy(newArr),1000);
        $("#table").bootstrapTable("checkBy", {field:"id", values:newArr})
    }

    function queryChecked(newArr){
        $("#table").bootstrapTable("checkBy", {field:"id", values:newArr});
    }

       $(function(){
        $('#table').bootstrapTable({
            locale:'zh-CN',
            onLoadSuccess:function(data){
                initCheck();
            }
        });

       });
    </script>


    <!--统计代码，可删除-->

</body>

</html>